package com.jzz.eletuanbackend.mapper;

import com.jzz.eletuanbackend.entity.Block;
import com.jzz.eletuanbackend.entity.Commodity;
import com.jzz.eletuanbackend.entity.Shop;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BusinessMapper {

    @Insert("INSERT INTO `shop` (businessId, contact, name, description, displayLocation, locationLongitude, " +
            "locationLatitude, licenseId, startPrice, shipPrice, working, withdraw) VALUES (" +
            "#{bid}, #{contact}, #{name}, #{description}, #{displayLocation}, #{long}, " +
            "#{lat}, #{licenseId}, #{startPrice}, #{shipPrice}, 0, 0)")
    Integer newShop(
            @Param("bid") Long bid,
            @Param("contact") String contact,
            @Param("name") String name,
            @Param("description") String description,
            @Param("displayLocation") String displayLocation,
            @Param("long") Double lng,
            @Param("lat") Double lat,
            @Param("licenseId") String licenseId,
            @Param("startPrice") Double startPrice,
            @Param("shipPrice") Double shipPrice
    );

    @Update("UPDATE `shop` SET working=0 WHERE `id`=#{id} AND NOT EXISTS ( SELECT * FROM v_order WHERE shopId=#{id} AND cancelTime='0' AND delivererTakeTime='0') LIMIT 1")
    Integer closeShop(@Param("id") Long id);

    @Update("UPDATE `shop` SET working=1 WHERE `id`=#{id} LIMIT 1")
    Integer openShop(@Param("id") Long id);

    @Update("UPDATE `shop` SET withdraw=1 WHERE `id`=#{id} LIMIT 1")
    Integer unregisterShop(@Param("id") Long id);

    @Select("SELECT * FROM `shop` WHERE businessId=#{bid} AND withdraw='0' ORDER BY id DESC")
    List<Shop> listShops(@Param("bid") Long bid);

    @Update("UPDATE `shop` SET description=#{desc}, startPrice=#{startPrice}, shipPrice=#{shipPrice} WHERE `id`=#{id} LIMIT 1")
    Integer editShop(
            @Param("id") Long id,
            @Param("startPrice") Double startPrice,
            @Param("shipPrice") Double shipPrice,
            @Param("desc") String desc
    );

    @Select("SELECT * FROM `shop` WHERE `id`=#{id} LIMIT 1")
    Shop getShop(@Param("id") Long id);

    @Insert("INSERT INTO `block` (`name`) VALUES (#{name})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer addBlock(Block block);

    @Insert("INSERT INTO `shop_block` (shopId, blockId) VALUES (#{sid}, #{bid})")
    Integer addBlockToShop(@Param("bid") Long bid, @Param("sid") Long sid);

    @Select("SELECT COUNT(1) FROM `v_block` WHERE shopId = #{sid} AND name = #{name} LIMIT 1")
    Integer existsBlockName(@Param("name") String name, @Param("sid") Long sid);

    @Delete("DELETE FROM `shop_block` WHERE blockId = #{bid} LIMIT 1")
    Integer removeBlockFromShop(@Param("bid") Long bid);

    @Insert("INSERT INTO `commodity` (name, image, description, specifications, price, minQuantity, packPrice)" +
            " VALUES (#{name}, #{image}, #{description}, #{specifications}, #{price}, #{minQuantity}, #{packPrice})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer newItem(Commodity commodity);

    @Insert("INSERT INTO `shop_commodity` (blockId, commodityId) VALUES (#{bid}, #{cid})")
    Integer addItemToShop(@Param("bid") Long bid, @Param("cid") Long cid);

    @Select("SELECT * FROM `v_commodity` WHERE `id` = #{id} LIMIT 1")
    Commodity getCommodity(@Param("id") Long id);

    @Select("SELECT * FROM block WHERE `id` = #{id} LIMIT 1")
    Block getBlockById(@Param("id") Long id);

    @Select("SELECT * FROM v_block WHERE shopId = #{id}")
    List<Block> getBlockByShopId(@Param("id") Long id);

    @Update("UPDATE `commodity` SET name = #{name}, image = #{image}, description = #{description}, " +
            "specifications = #{specifications}, price = #{price}, minQuantity = #{minQuantity}, packPrice = #{packPrice} WHERE id = #{id} LIMIT 1")
    Integer editItem(Commodity commodity);

    @Update("UPDATE `shop_commodity` SET blockId = #{bid} WHERE commodityId = #{cid} LIMIT 1")
    Integer changeItemBlock(@Param("cid") Long commId, @Param("bid") Long blockId);

    @Delete("DELETE FROM `commodity` WHERE `id` = #{id} LIMIT 1")
    Integer removeItem(Long id);

    @Select("SELECT `id` FROM `shop` WHERE `businessId` = #{uid}")
    List<Long> allShopIds(@Param("uid") Long uid);
}
